//  ========== 
//  = 公用JS = 
//  ========== 
//测试环境
/*var h5_root_url = 'http://139.196.10.201:8001'; //当前h5部署的地址
var server_root = 'http://139.196.10.201:18082'; //服务请求的根节点
var server_url = server_root + '/p2p_test';*/
//本地环境
/*var h5_root_url = 'http://192.168.16.42:8010';//当前h5部署的地址
var server_root = 'http://192.168.16.42:8000';//服务请求的根节点
var server_url = server_root+'/sh_p2p_bhtz';*/
//线上环境
var h5_root_url = 'http://app.binhetouzi.com:8000';
var server_root = 'http://back.binhetouzi.com:18081';//服务请求的根节点
var server_url = server_root+'';
var storage = window.localStorage;
var timeout = 10000;
var networktag = 0;
var isMobile = 1; //移动端标识
var isH5 = !mui.os.android && !mui.os.ios;
var memIDFourth = '3'; //微信 2，安卓 3，ios 4，pc 1.  6位，除了第4位有固定值，其他的要随机
function network() {
	var a = {};
	a[plus.networkinfo.CONNECTION_NONE] = "未连接网络";
	var b = a[plus.networkinfo.getCurrentType()];
	if(b == "未连接网络") {
		plus.nativeUI.closeWaiting();
		mui.toast("未连接网络")
	} else {
		networktag = 1
	}
}
var havereturn = false;

function ontimeout() {
	if(!havereturn) {
		plus.nativeUI.closeWaiting();
		mui.toast("连接超时请检查网络连接或重试");
		havereturn = true
	}
}

window.Util = (function(a) {
	//用户等级范围
	a.creditRate = {
		1: 'HR',
		2: 'E',
		3: 'D',
		4: 'C',
		5: 'B',
		6: 'A',
		7: 'AA'
	};
	a.getSoftWareId = function() {
		return "6"
	};
	a.setItem = function(key, data) {
		storage.setItem(key, data);
	}
	a.getItem = function(key) {
		return storage.getItem(key);
	}
	a.getLoginUser = function() {
		var user = {};
		user.memberId = storage.getItem("memberId");
		//		user.memberId = '746';
		/*user.memberId = '712';*/
		user.loginName = storage.getItem("loginname");
		user.trueName = storage.getItem("truename");
		user.telphone = storage.getItem("telphone");
		return user;
	};
	/**
	 *判断是否登录 true-已登录 
	 */
	a.checkLogin = function() {
		var b = storage.getItem("memberId");
		if(b) {
			return true
		} else {
			return false
		}
	};
	/**
	 * 判断用户未登录跳转login.html
	 */
	a.checkLoginRedirect = function() {
		if(!checkLogin()) {
			mui.openWindow("login.html");
			return false;
		}
		return true;
	}
	/**
	 *获取请求路径参数信息 
	 * @param {Object} d
	 * @param {Object} g
	 */
	a.requestURL = function(d, g) {
		var c = location.href;
		if(g) {
			c = g
		}
		var e = c.substring(c.indexOf("?") + 1, c.length).split("&");
		var b = {};
		for(i = 0; j = e[i]; i++) {
			b[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length)
		}
		var f = b[d.toLowerCase()];
		if(typeof(f) == "undefined") {
			return ""
		} else {
			return decodeURI(f)
		}
	};
	a.safeString = function(b, replaceStr) {
		if(b == undefined || b == null) {
			if(replaceStr) {
				return replaceStr;
			}
			return "";
		}
		return b
	};

	a.getThirdPayLink = function(url) {
		window.open(url, '_system', 'location=yes');
	};
	a.loading = function() {
		layer.open({ type: 2 });
	}
	a.closeLoading = function() {
		layer.closeAll();
	}
	a.toast = function(message) {
		mui.toast(message);
	}
	a.openMessage = function(message, btn) {
		layer.open({
			content: message,
			btn: btn ? btn : '确定'
		});
	}
	a.memID = function() {
		return parseInt(Math.random() * 1000) + memIDFourth + parseInt(Math.random() * 100);
	}
	return a;
})(window);

window.Util.time = {
	getLastDay: function() {
		var c = new Date();
		var b = function(e) {
			e += "";
			return e.replace(/^(\d)$/, "0$1")
		};
		var a = new Date(c.getFullYear(), parseInt(c.getMonth()) + 1, 0);
		var d = c.getFullYear() + "-" + b(parseInt(c.getMonth()) + 1) + "-" + a.getDate();
		return d
	},
	getFirstDay: function() {
		var b = new Date();
		var a = function(d) {
			d += "";
			return d.replace(/^(\d)$/, "0$1")
		};
		var c = b.getFullYear() + "-" + a(parseInt(b.getMonth()) + 1) + "-01";
		return c
	},
	formatDate: function(c, d) {
		var b = function(e) {
			e += "";
			return e.replace(/^(\d)$/, "0$1")
		};
		var a = {
			yyyy: c.getFullYear(),
			yy: c.getFullYear().toString().substring(2),
			M: c.getMonth() + 1,
			MM: b(c.getMonth() + 1),
			d: c.getDate(),
			dd: b(c.getDate()),
			hh: c.getHours(),
			mm: c.getMinutes(),
			ss: c.getSeconds()
		};
		d || (d = "yyyy-MM-dd hh:mm:ss");
		return d.replace(/([a-z])(\1)*/ig, function(e) {
			return a[e]
		})
	}

}

$(function() {
	//底部导航切换
	$('#G-footer-nav a').on('tap', function(e) {
		footer_nav(e);
	})
	/**跳转个人信息*/
	$("#info").on('tap', function() {
		if(checkLoginRedirect()){
			openTab('../page/user.html', 'user');
		}
	});
});
var footer_nav = function(e) {
	var id = $(e.target).closest('a').attr('id'); //获取点击导航ID
	switch(id) {
		case 'G-nav-home':
			openTab('../page/main.html', 'main');
			break;
		case 'G-nav-financing':
			openTab('../page/financeList.html', 'financeList');
			break;
		case 'G-nav-personal':
			if(checkLoginRedirect()) {
				openTab('../page/user.html', 'user');
			}
			break;
		case 'G-nav-activity':
			window.location.href = '活动.html';
			break;
	}
};
//点击下方tab切换
function openTab(url, id) {
	mui.openWindow(url, id);
	if(!isH5 && window.plus) {
		var currWebview = plus.webview.getWebviewById(id);
		currWebview.evalJS("activeTap();");
	}
}

function openFinanceListTab(url, id, index) {
	mui.openWindow(url, id);
	if(!isH5 && window.plus) {
		var currWebview = plus.webview.getWebviewById(id);
		if(index && index == 1) {
			currWebview.evalJS("activeTap1();");
		} else {
			currWebview.evalJS("activeTap();");
		}

	}
}
